/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewStatistics.java
 *
 * Created on 2009-11-29, 21:20:11
 */

package clientapp;

import controller.ScoreControl;
import ejb.GameUser;
import ejb.Score;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author automne
 */
public class ViewStatistics extends javax.swing.JFrame {

    private GameUser player;

    /** Creates new form ViewStatistics */
    public ViewStatistics() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    public ViewStatistics(GameUser player) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.player = player;
        rankByTotal();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tableRank = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        comboOrder = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        lblScore = new javax.swing.JLabel();
        txtRank = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtScore = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tableRank.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Rank", "Username", "Total Score", "Highest Score", "Last Score"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableRank.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tableRank);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 3, 24));
        jLabel1.setText("Player Statisitics");

        comboOrder.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Total Score", "Highest Score" }));
        comboOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboOrderActionPerformed(evt);
            }
        });

        jLabel2.setText("Order By");

        jButton1.setText("Back");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        lblScore.setText("Your Total Score:");

        txtRank.setEditable(false);
        txtRank.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtRank.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtRankActionPerformed(evt);
            }
        });

        jLabel4.setText("Your Rank:");

        txtScore.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(120, 120, 120))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(20, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblScore)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtScore, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtRank, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblScore)
                    .addComponent(jButton1)
                    .addComponent(txtRank, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtScore, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtRankActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtRankActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtRankActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        new PlayerMain(player).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void comboOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboOrderActionPerformed
        // TODO add your handling code here:
        int choice = this.comboOrder.getSelectedIndex();
        if (choice == 0) {
            rankByTotal();
        }
        if (choice == 1) {
            rankByHighest();
        }
    }//GEN-LAST:event_comboOrderActionPerformed

    public void rankByTotal() {
        ScoreControl cScore = new ScoreControl("ejb.ScoreFacade");
        List<Score> scoreList = cScore.DisplayTotalScore();
        ListIterator<Score> iter = scoreList.listIterator();
        int rowNum = 0;
        while (iter.hasNext() == true) {
            Score temp = iter.next();
            this.tableRank.setValueAt(rowNum+1, rowNum, 0);
            this.tableRank.setValueAt(temp.getId(), rowNum, 1);
            this.tableRank.setValueAt(temp.getTotalScore(), rowNum, 2);
            this.tableRank.setValueAt(temp.getHighestScore(), rowNum, 3);
            this.tableRank.setValueAt(temp.getLastScore(), rowNum, 4);
            if (temp.getId().equals(player.getId()) == true) {
                this.lblScore.setText("Your Total Score:");
                this.txtScore.setText(String.valueOf(temp.getTotalScore()));
                this.txtRank.setText(String.valueOf(rowNum + 1));
            }
            rowNum ++;
            //System.out.println("UserName: " + temp.getId() + '\n' + "Highest Score: " + temp.getHighestScore());
        }
    }

    public void rankByHighest() {
        ScoreControl cScore = new ScoreControl("ejb.ScoreFacade");
        List<Score> scoreList = cScore.DisplayHighestScore();
        ListIterator<Score> iter = scoreList.listIterator();
        int rowNum = 0;
        while (iter.hasNext() == true) {
            Score temp = iter.next();
            this.tableRank.setValueAt(rowNum + 1, rowNum, 0);
            this.tableRank.setValueAt(temp.getId(), rowNum, 1);
            this.tableRank.setValueAt(temp.getTotalScore(), rowNum, 2);
            this.tableRank.setValueAt(temp.getHighestScore(), rowNum, 3);
            this.tableRank.setValueAt(temp.getLastScore(), rowNum, 4);
            if (temp.getId().equals(player.getId()) == true) {
                this.lblScore.setText("Your Highest Score:");
                this.txtScore.setText(String.valueOf(temp.getHighestScore()));
                this.txtRank.setText(String.valueOf(rowNum + 1));
            }
            rowNum ++;
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewStatistics().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboOrder;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblScore;
    private javax.swing.JTable tableRank;
    private javax.swing.JTextField txtRank;
    private javax.swing.JTextField txtScore;
    // End of variables declaration//GEN-END:variables

}
